package com.hot100.easy;

import java.util.HashMap;

/**
 * @ClassName Demo14
 * @Description  多数元素
 * @Author zt
 * @Date 15:35 2022/6/11
 * @Version 1.0
 **/
public class Demo14 {
    public static void main(String[] args) {

    }
    public int majorityElement(int[] nums) {
        int mid =nums.length/2;
        HashMap<Integer,Integer> hashMap=new HashMap<Integer, Integer>();
        for (int num : nums) {
            if(hashMap.containsKey(num)){
                Integer integer = hashMap.get(num);
                hashMap.put(num,++integer);
            }else {
                hashMap.put(num,1);
            }
            Integer integer = hashMap.get(num);
            if(integer>mid){
                return num;
            }
        }
        return 0;
    }
}